package com.eeprint.system.service.platform.impl;

import com.eeprint.system.domain.platform.FORM.PlatformUserUpdateFORM;
import com.eeprint.system.domain.platform.PlatformUser;
import com.eeprint.system.mapper.platform.PlatformUserMapper;
import com.eeprint.system.service.platform.PlatformUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;

/**
 * @Author newhonor
 * @Date 2020/10/19 10:46
 * @Version 1.0
 **/
@Service
public class PlatformUserServiceImpl implements PlatformUserService {

    @Autowired
    private PlatformUserMapper platformUserMapper;

    @Override
    public BigInteger findRelAdminUserIdByPhone(String tenantAdminPhone) {
        return platformUserMapper.findRelAdminUserIdByPhone(tenantAdminPhone);
    }

    @Override
    public PlatformUser findPlatformUserById(BigInteger id) {
        return platformUserMapper.findPlatformUserById(id);
    }

    @Override
    public boolean updatePlatformUserPassword(PlatformUserUpdateFORM platformUserUpdateFORM) {
        String oldPassword = platformUserUpdateFORM.getUserPasswordOld();
        String newPassword = platformUserUpdateFORM.getUserPasswordNew();
        String checkPassword = platformUserUpdateFORM.getUserPasswordCheck();

        //检查确认密码与新密码是否一致
        if(!checkPassword.equals(newPassword)) return false;

        //检查原密码和数据库密码是否一致
        String currentPassword = findPlatformUserById(platformUserUpdateFORM.getId()).getUserPassword();
        if(!currentPassword.equals(oldPassword)) return false;

        PlatformUser platformUser = new PlatformUser();
        platformUser.setId(platformUserUpdateFORM.getId());
        platformUser.setUserPassword(platformUserUpdateFORM.getUserPasswordNew());

        platformUser.setUpdateAt(new Date());
        platformUser.setUpdateUserId(BigInteger.valueOf(98877));//测试

        platformUserMapper.updatePlatformUserPassword(platformUser);

        return true;

    }


}
